﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace APNS.NET
{

    public enum NotificationStatusCode : byte
    {
        NoErrorsEncountered = 0,
        ProcessingError = 1,
        MissingDeviceToken = 2,
        MissingTopic = 3,
        MissingPayload = 4,
        InvalidTokenSize = 5,
        InvalidTopicSize = 6,
        InvalidPayloadSize = 7,
        InvalidToken = 8,
        NoneUnknown = 255
    }


    // Represents a response that is read from the APNS service once a notification 
    // has been sent. The response contains a notification id that identifies the notification (user defined)
    // and a status code.
    public class ApnsNotificationResponse
    {
       
        private const int NotificationResponseCommand = 8;

        public int NotificationId { get; private set; }
        public NotificationStatusCode StatusCode { get; private set; }

        private ApnsNotificationResponse()
        {            
        }

        internal ApnsNotificationResponse(int notificationId, NotificationStatusCode statusCode)
        {
            this.NotificationId = notificationId;
            this.StatusCode = statusCode;
        }

        internal static ApnsNotificationResponse FromByteArray(byte[] buffer)
        {
            if (buffer == null) 
                throw new ArgumentNullException("buffer");

            ApnsNotificationResponse result = null;
            if (buffer.Length >= 6)
            {
                byte command = buffer[0];
                if(command==NotificationResponseCommand)
                {
                    result = new ApnsNotificationResponse() {
                                    NotificationId = BitConverter.ToInt32(buffer, 2),
                                    StatusCode = (NotificationStatusCode)buffer[1]
                                };
                }
            }
            return result;
        }
    }
}
